package jdbc;

import jdbc.common.InputUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class JDBCDemo11 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String userName = scan.nextLine();
        System.out.println("请输入新密码:");
        String passWord = scan.nextLine();
        System.out.println("请输入新昵称:");
        String nickName = scan.nextLine();
        try (Connection connection = DBUtil.getConnection();){
            String sql = "UPDATE userinfo SET password = ?,nickname = ?" +
                    "WHERE username = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, passWord);
            ps.setString(2, nickName);
            ps.setString(3, userName);
            int num = ps.executeUpdate();
            System.out.println(num > 0 ? "更改成功" : "用户名或密码错误");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
